<?php

namespace app\api\controller\xykeep;
use app\common\controller\Api;

use app\api\model\xykeep\goods\Order as GoodsOrderModel;
use app\common\model\Comment as CommentModel;
use app\common\model\Store as StoreModel;
use app\common\validate\Comment as CommentValidate;
use think\Db;

/**
 * XYkeep 商城订单接口
 */
class GoodsOrder extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    

    /**
	 * 列表
	 */
	public function lists()
    {
    	$params = $this->request->post();
        $data = GoodsOrderModel::getLists($params);
        $this->success('分销订单列表', $data);
    }

    /**
	 * 加载（确认订单数据）
	 */
	public function init()
    {
    	$params = $this->request->post();
        //是否是商家
        $user_id = $this->auth->id;
        $user_phone = Db::name('user')->where('id', $user_id)->value('mobile');
        $storeInfo = StoreModel::where([
            'phone' => $user_phone,
        ])->find();
        //获得N个商品的所有store_id
        $order_goods_list = $params['goods_list'];
        $goods_ids = array_column($order_goods_list, 'goods_id');
        $store_ids = Db::name('xykeep_goods')->where([
            'id' => ['in',$goods_ids],
        ])->column('store_id');
        if(in_array(0,$store_ids) && !$storeInfo){
            $this->error('非商家用户不允许下单平台商品');
        }
        if(!in_array(0,$store_ids) && $storeInfo){
            $this->error('商家只允许下单平台商品');
        }

        $data = GoodsOrderModel::getInitData($params);
        $this->success('订单数据', $data);
    }

    /**
     * 创建
     */
	public function add()
    {
        $params = $this->request->post();
        $order = GoodsOrderModel::addOrder($params);
        $this->success('订单创建成功', $order);
    }

    /**
     * 详情
     */
    public function detail()
    {
        $params = $this->request->post();
        $this->success('订单详情', GoodsOrderModel::getDetail($params));
    }

    public function cancel()
    {
        $params = $this->request->post();
        $this->success('取消成功', GoodsOrderModel::cancelOrder($params));
    }


    /**
     * 物流查询
     */
    public function logistics()
    {
        $params = $this->request->post();
        $this->success('物流查询', GoodsOrderModel::getLogistics($params));
    }

    /**
     * 确认收货
     */
    public function take_delivery(){
        $params = $this->request->post();
        $this->success('收货成功', GoodsOrderModel::takeDelivery($params));
    }

    //订单提交评价
    public function comment()
    {
        $param = $this->request->post();
        $user_id = $this->auth->id;

        $param['user_id'] = $user_id;

        $goods_order = (new GoodsOrderModel())->where([
            'id' => $param['order_id'],
            'user_id' => $user_id
        ])->find();

        if(!$goods_order){
            $this->error('订单不存在');
        }

        $goods_order_items = Db::name('xykeep_goods_order_item')->where([
            'goods_order_id' => $param['order_id'],
        ])->select();
        if(!$goods_order_items){
            $this->error('订单商品不存在');
        }

        $before_comment = CommentModel::where([
            'order_id' => $param['order_id'],
        ])->find();

        if($before_comment){
            $this->error('你已经评价过了');
        }


        Db::startTrans();
        try{
            $param['store_id'] = $goods_order['store_id'];

            $commentModel = new CommentModel();

            foreach ($goods_order_items as $goods_order_item) {
                $param['target_id'] = $goods_order_item['goods_id'];
                $validate = new CommentValidate();
                if (!$validate->scene('goods_add')->check($param)) {
                    throw new \Exception($validate->getError());
                }

                $comment_insert = [
                    'user_id' => $user_id,
                    'target_id' => $param['target_id'],
                    'store_id' => $param['store_id'],
                    'order_id' => $param['order_id'],
                    'star' => $param['star'],
                    'content' => $param['content'],
                    'type' => 'goods'
                ];

                $commentModel->save($comment_insert);
            }

            $goods_order->status = 5;
            $goods_order->save();

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('评论成功');
    }
	
}